import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as mqtt from 'mqtt'

@Injectable()
export class MqttService {
    constructor(
        private configService:ConfigService
    ) {
    }

    connect() :Promise<mqtt.MqttClient>{
        const conf = this.configService.get('mqtt')

        return new Promise((resolve, reject) => {
            const client = mqtt.connect(conf.options)
            client.on('error', error => {
                reject(error)
            }).on('connect', () => {
                resolve(client)
            })
        })

    }
}
